import qqmapsdk from '../../../utils/qqmap'

Page({
  data: {

  },

  onLoad() {
    this.getLocation()
  },
  async getLocation() {
    const { latitude, longitude } = await wx.getLocation()
    this.getPoint(latitude, longitude)
  },
  async chooseLocation() {
    // 用户所在位置经纬度
    const { latitude, longitude } = await wx.chooseLocation()
    // 查看经纬度
    // console.log(latitude, longitude)
    this.getPoint(latitude, longitude)
  },

  getPoint(latitude, longitude) {
    // 加载进度条
    wx.showLoading({ title: '正在加载...', mask: true })

    qqmapsdk.reverseGeocoder({
      location: [latitude,longitude].join(','),
      success: ({ result: {address} }) => {
        this.setData({address })
        // console.log(res);
      }
    })

    qqmapsdk.search({
      keyword: '住宅小区',  //搜索关键词
      location: [latitude,longitude].join(','),  //设置周边搜索中心点
      page_size: 5,
      success: (res) => {
        const points = res.data.map(({ id,title }) => {
          return {id,title}
        })
        this.setData({points})
        console.log(points);
      },

      complete: () => {
        // 隐藏加载状态
        wx.hideLoading()
      }
      
    })

    
  },
  
  goBuilding(e) {
    console.log(e);
    wx.navigateTo({
      url: `/house_pkg/pages/building/index?point=${e.mark.point}`,
    })
  }
})